package net.csdra.wenhua.nanan.qiandao.service;

import net.csdra.wenhua.nanan.qiandao.domain.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by xiaomi on 2017/2/21.
 */
@Service
public class AdminServiceImpl implements AdminService {

    private static final Logger log = LoggerFactory.getLogger(AdminServiceImpl.class);

    private final String salt;
    private final String username;
    private final String password;
    private final UserRepository userRepository;
    private final CheckInRepository checkInRepository;

    @Autowired
    public AdminServiceImpl(@Value("${admin.salt}") String salt, @Value("${admin.username}") String username, @Value("${admin.password}") String password,
                            UserRepository userRepository, CheckInRepository checkInRepository) {
        this.salt = salt;
        this.username = username;
        this.password = password;
        this.userRepository = userRepository;
        this.checkInRepository = checkInRepository;
    }

    @Override
    public boolean adminLogin(AdminLogin adminLogin) {
        log.debug("enter adminLogin: {}", adminLogin);
        Assert.notNull(adminLogin.username);
        if (adminLogin.username.equals(username) && adminLogin.password.equals(password.replace(salt, ""))) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean deleteUserByUserId(String userId) {
        Assert.notNull(userId);
        userRepository.delete(userId);
        log.debug("deleteUserByUserId: userId=", userId);
        return true;
    }

    @Override
    public boolean deleteCheckInByCheckInId(String checkInId) {
        Assert.notNull(checkInId);
        checkInRepository.delete(checkInId);
        log.debug("deleteCheckInByCheckInId, checkInId=", checkInId);
        return true;
    }

    @Override
    public User getUserByWeixinId(String weixinId) {
        Assert.notNull(weixinId);
        List<User> userList = userRepository.findByWeixinId(weixinId);
        User user = null;
        if (userList.size() > 0) {
            user = userList.get(0);
        }
        log.debug("getUserByWeixinId, {}", user);
        return user;
    }

    @Override
    public Page<Record> getPagedRecord(GetPagedRecord getPagedRecord) {
        log.debug("enter getPagedRecord, {}", getPagedRecord);
        int page = getPagedRecord.getPage();
        int size = getPagedRecord.getSize();
        Page<Record> resultPage = userRepository.findRecords(getPagedRecord.getFrom(), getPagedRecord.getTo(), new PageRequest(page, size));

//        List<Record> recordList = resultPage.getContent().stream().map(m -> {
//            Record record = new Record();
//            record.setUserId((String) m.get("userId"));
//            record.setUserInfo((String) m.get("userInfo"));
//            record.setTotalCheckInNum((int) m.get("totalCheckInNum"));
//            return record;
//        }).collect(Collectors.toList());

        return resultPage;
    }

    @Override
    public List<CheckIn> getCheckInListAfter(String userId, Timestamp after) {
        return checkInRepository.findByUserIdAndCheckInTimeAfter(userId, after);
    }
}
